﻿Imports System.Data.SqlClient

Public Class Frm_Idafa_khossoum

    Public dv As Dossier
    Public Enm As Enemie
    Public cltDoss As Dossier_Adversaire
    Dim dossAvo As Dossier_Avocat_Adversaire
    Dim deChod As DictionaryEntry
    Public TemDoss As Dossier_Avocat_Adversaire
    Dim ListViewItem1 As System.Windows.Forms.ListViewItem
    

    Public Sub remplirMo7ami()
        Partager.moi.remplir_List_avocat_enemie()

        Dim htClient As New Hashtable()
        Dim str As String = ""
        For Each clt As Avocat_enemie In Partager.moi.List_avocat_enemie
            str = clt.Nom_a & "  " & clt.Prenom_a
            htClient.Add(clt.Id, str)
        Next

        For Each ImageType As DictionaryEntry In htClient
            Comb_mohami.Items.Add(ImageType)
        Next
        Comb_mohami.DisplayMember = "value"
        Comb_mohami.ValueMember = "key"
        If Comb_mohami.Items.Count > 0 Then
            Comb_mohami.SelectedIndex = 0
        End If
    End Sub
    Public Sub Replirlistmohami()
        List_mohami.Items.Clear()
        For Each khib As Dossier_Avocat_Adversaire In dv.List_avocat_enemie
            For Each clt As Avocat_enemie In Partager.moi.List_avocat_enemie
                If khib.Id_Avocat_Adversaire = clt.Id Then

                    ListViewItem1 = New System.Windows.Forms.ListViewItem(New String() {"", clt.Nom_a & " " & clt.Prenom_a, clt.Id}, -1)
                    'il est indispensable d'ajouter chekHand false et true
                    Partager.checkHand = False
                    If khib.nomme Then
                        ListViewItem1.Checked = True
                    Else
                        ListViewItem1.Checked = False
                    End If
                    Me.List_mohami.Items.AddRange(New System.Windows.Forms.ListViewItem() {ListViewItem1})
                    Partager.checkHand = True
                End If
            Next
        Next

    End Sub

    Private Sub Btn_parc_marji3ona_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Enabled = False
        Frm_Rech_Dossier.Show()
        Partager.type = "Frm_Idafa_khossoum"
    End Sub

    Private Sub Btn_parc_nom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_nom.Click
        Me.Enabled = False
        Frm_Rech_Adver.Show()
        Partager.type_rech_adver = "Frm_Idafa_khossoum"
    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Enabled = False
        Frm_Rech_Avo_Adver.Show()
        Partager.type_rech_Avo_adver = "Frm_Idafa_khossoum"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Btn_mokhabara_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_mokhabara.Click
        If Comb_marji3ona.SelectedIndex >= 0 And List_mohami.SelectedIndices.Count > 0 Then
            Frm_Mohami_mokhabara.MdiParent = Frm_Main
            Me.Enabled = False
            Frm_Mohami_mokhabara.Show()
        Else
            MsgBox("يجب عليك اختيار الملف و المحامي معا", MsgBoxStyle.Critical, "خطأ")
        End If
    End Sub

    Private Sub Frm_Idafa_khossoum_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Partager.nonInitial = New ArrayList
        Partager.nonInitial.Add("Comb_marji3ona")
        Partager.nonInitial.Add("Comb_nom")

        List_mohami.Columns(0).DisplayIndex = List_mohami.Columns.Count - 1
        Comb_siffa.SelectedIndex = 0
        Partager.close()
        Try
            '----------------------------------------------------------------
            Partager.remplirEnemie(Comb_nomf)
            '----------------------------------------------------------------
            remplirMo7ami()
            '----------------------------------------------------------------
            Partager.remplirClients(Comb_nom)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        '------------------------------------------------------------------------
    End Sub

    Private Sub Btn_ajout_khaassm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout_khaassm.Click
        If Comb_marji3ona.SelectedIndex < 0 Then
            MsgBox("اختر الملف أولا", MsgBoxStyle.Critical, "خطأ في الإضافة")
        Else
            Try
                If MsgBox("هل تريد فعلا إضافة هذا الخصم لهذا الملف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة") = MsgBoxResult.Yes Then
                    Partager.close()
                    Partager.deClt = DirectCast(Comb_nomf.SelectedItem, DictionaryEntry)
                    Dim idClt As Integer = CInt(Partager.deClt.Key)
                    cltDoss = New Dossier_Adversaire(dv.id, idClt, Comb_siffa.Text)
                    dv.Ajouter_enemi(cltDoss)

                    List_mowakilin.Items.Clear()

                    Dim str As String = ""
                    For Each khib As Dossier_Adversaire In dv.List_enemi
                        For Each Enm As Enemie In Partager.moi.List_enemi
                            If khib.Id_Adversaire = Enm.Id Then
                                If Enm.Type = "ذاتي" Then
                                    str = Enm.Nom & "  " & Enm.Prenom
                                ElseIf Enm.Type = "معنوي" Then
                                    str = Enm.Nom_ent
                                End If
                                List_mowakilin.Items.Add(New ListViewItem(New String() {str, khib.type}))
                            End If
                        Next
                    Next
                    MsgBox("تمت إضافة الخصم للملف بنجاح", MsgBoxStyle.Information, "نجاح")
                End If
            Catch sqlEx As SqlException
                If sqlEx.Message.Contains("PK_Dossier_Adversaire") Then
                    MsgBox("عفوا، هذا الخصم تمت إضافته لهذاالملف من قبل", MsgBoxStyle.Critical, "خطأ")
                Else
                    MsgBox(sqlEx.Message)
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
    End Sub

    Private Sub Btn_supp_khaassm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp_khaassm.Click
        If List_mowakilin.SelectedIndices.Count > 0 Then
            If MsgBox("هل تريد فعلا حذف هذا الخصم من هذا الملف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف") = MsgBoxResult.Yes Then
                Partager.close()
                Try
                    Dim cltDoss As Dossier_Adversaire = dv.List_enemi(List_mowakilin.SelectedIndices.Item(0))
                    dv.suprimmer_enemi(cltDoss.Id_Adversaire)

                    List_mowakilin.Items.Clear()
                    Dim str As String = ""
                    For Each khib As Dossier_Adversaire In dv.List_enemi
                        For Each Enm As Enemie In Partager.moi.List_enemi
                            If khib.Id_Adversaire = Enm.Id Then
                                If Enm.Type = "ذاتي" Then
                                    str = Enm.Nom & "  " & Enm.Prenom
                                ElseIf Enm.Type = "معنوي" Then
                                    str = Enm.Nom_ent
                                End If
                                List_mowakilin.Items.Add(New ListViewItem(New String() {str, khib.type}))
                            End If
                        Next
                    Next

                    MsgBox("عملية الحذف تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        Else
            MsgBox("اختر خصم من القائمة", MsgBoxStyle.Information, "خطأ")
        End If
    End Sub

    Private Sub Btn_ajout_mohami_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout_mohami.Click
        If Comb_marji3ona.SelectedIndex < 0 Then
            MsgBox("اختر المرجع أولا", MsgBoxStyle.Critical, "خطأ في الإضافة")
        Else
            If Comb_mohami.Text <> "" Then

                Try
                    If MsgBox("هل تريد فعلا إضافة هذا المحامي لهذا الملف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة") = MsgBoxResult.Yes Then
                        Partager.close()
                        deChod = DirectCast(Comb_mohami.SelectedItem, DictionaryEntry)
                        Dim idmohami As Integer = CInt(deChod.Key)


                        TemDoss = New Dossier_Avocat_Adversaire(idmohami, True)
                        dv.Ajouter_avocat_enemie(TemDoss)

                        Replirlistmohami()

                        MsgBox("تمت إضافة المحامي للملف بنجاح", MsgBoxStyle.Information, "نجاح")
                    End If
                Catch sqlEx As SqlException
                    If sqlEx.Message.Contains("PK_Dossier_Avocat_Adversaire") Then
                        MsgBox("عفوا، هذا المحامي تمت إضافته لهذاالملف من قبل", MsgBoxStyle.Critical, "خطأ")
                    Else
                        MsgBox(sqlEx.Message)
                    End If
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        End If
    End Sub

    Private Sub Btn_supp_mohami_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp_mohami.Click
        If List_mohami.SelectedIndices.Count > 0 Then
            If MsgBox("هل تريد فعلا حذف هذا المحامي من هذا الملف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف") = MsgBoxResult.Yes Then
                Partager.close()
                Try
                    Dim cltDoss As Dossier_Avocat_Adversaire = dv.List_avocat_enemie(List_mohami.SelectedIndices.Item(0))
                    dv.suprimmer_avocat_enemie(cltDoss)

                    Replirlistmohami()

                    MsgBox("عملية الحذف تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        Else
            MsgBox("اختر محامي من القائمة", MsgBoxStyle.Information, "خطأ")
        End If
    End Sub

    Private Sub List_mohami_ItemChecked(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles List_mohami.ItemChecked
        'Dim dossAvo As dossAvoBur = dv.List_avocat_bureau(List_mowakilin.SelectedIndices.Item(0))
        If Partager.checkHand Then
            Dim i As Integer = 0
            For Each item1 As ListViewItem In List_mohami.Items
                dossAvo = New Dossier_Avocat_Adversaire
                dossAvo = dv.List_avocat_enemie(i)
                dossAvo.nomme = item1.Checked
                dv.modifierAvoEnemNomme(dossAvo)
                i += 1
            Next
        End If
    End Sub

    Private Sub List_mohami_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_mohami.SelectedIndexChanged
        For Each i As Integer In List_mohami.SelectedIndices
            Partager.idAvoDoss = CInt(List_mohami.SelectedItems(0).SubItems(2).Text)
            Partager.nomMohami = List_mohami.SelectedItems(0).SubItems(1).Text
        Next
    End Sub

    Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Comb_nom.KeyPress
        e.Handled = True
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_nom.SelectedIndexChanged
        List_mowakilin.Items.Clear()
        List_mohami.Items.Clear()
        Partager.deClt = DirectCast(Comb_nom.SelectedItem, DictionaryEntry)
        Partager.remplirDossier(Comb_marji3ona, CInt(Partager.deClt.Key))
    End Sub

    Private Sub Comb_marji3ona_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_marji3ona.SelectedIndexChanged
        Try
            Partager.deClt = DirectCast(Comb_marji3ona.SelectedItem, DictionaryEntry)
            For Each d As Dossier In Partager.moi.List_info_fichier
                If d.id = CInt(Partager.deClt.Key) Then
                    dv = d
                    dv.remplir_List_enemi()
                    dv.remplir_List_avocat_enemie()
                    Exit For
                End If
            Next
            List_mowakilin.Items.Clear()
            Dim str As String = ""
            For Each khib As Dossier_Adversaire In dv.List_enemi
                For Each Enm As Enemie In Partager.moi.List_enemi
                    If khib.Id_Adversaire = Enm.Id Then
                        If Enm.Type = "ذاتي" Then
                            str = Enm.Nom & "  " & Enm.Prenom
                        ElseIf Enm.Type = "معنوي" Then
                            str = Enm.Nom_ent
                        End If
                        List_mowakilin.Items.Add(New ListViewItem(New String() {str, khib.type}))
                    End If
                Next
            Next

            Replirlistmohami()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class